#!/usr/bin/env python


import config


def singleton(klass):
    instances = {}
    def get_instance():
        if klass not in instances:
            instances[klass] = klass()
        return instances[klass]
    return get_instance


class _CommonDict(dict):
    """Code common to the various dicts that we parse out of Unilex."""

    dict_type = None

    def __init__(self, path_to_unilex=config.PATH_TO_UNILEX):
        """Instantiate and populate this current dict."""

        super(dict, self).__init__()

        assert self.dict_type in ('pos', 'pron', 'ortho', 'freq',)

        with open(path_to_unilex) as f:
            for line in f.readlines():
                word, pron_num, pos, pron, ortho, freq = line.split(':')
                value = locals()[self.dict_type]
                try:
                    self[word].append(value)
                except KeyError:
                    self[word] = [value]


@singleton
class POSDict(_CommonDict):
    dict_type = 'pos'

@singleton
class PronDict(_CommonDict):
    dict_type = 'pron'

@singleton
class OrthoDict(_CommonDict):
    dict_type = 'ortho'

@singleton
class FreqDict(_CommonDict):
    dict_type = 'freq'
